<?php
/**
 * @title 1818. 绝对差值和 - 二分查找
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $nums1
     * @param Integer[] $nums2
     * @return Integer
     */
    function minAbsoluteSumDiff($nums1, $nums2) {
        /**
         * @since 2021-07-15 长度
         */
        $len = count($nums1);

        /**
         * @since 2021-07-15 差值
         */
        $diffSum = 0;

        /**
         * @since 2021-07-15 哈希, 等于判断
         */
        $numHashArray = [];
        foreach ($nums1 as $num){
            $numHashArray[$num] = 1;
        }

        /**
         * @since 2021-07-15 排序, 便于二分查找
         */
        $num3 = array_keys($numHashArray);
        sort($num3);
        $len3 = count($num3);

        /**
         * @since 2021-07-15 减少最大差值
         */
        $maxDiff = 0;

        /**
         * @since 2021-07-15 计算差值
         */
        for ($i=0; $i<$len; $i++){
            /**
             * @since 2021-07-15 差值
             */
            $target = $nums2[$i];
            $diff = abs($nums1[$i] - $target);

            /**
             * @since 2021-07-15 差值和
             */
            $diffSum += $diff;

            /**
             * @since 2021-07-15 尝试减少差值
             */
            if($diff > $maxDiff){
                /**
                 * @since 2021-07-15 查找target
                 */
                if(isset($numHashArray[$target])){
                    $maxDiff = $diff;
                } else {
                    /**
                     * @since 2021-07-15 查找区间, s < target < e
                     */
                    $s = 0;
                    $e = $len3-1;
                    while($e-$s > 1){
                        $mid = ($s+$e)>>1;
                        $val = $num3[$mid];

                        /**
                         * @since 2021-07-15 val不可能等于target
                         */
                        if($val > $target){
                            $e = $mid;
                        } else {
                            $s = $mid;
                        }
                    }
                }

                /**
                 * @since 2021-07-15 取最大, 减少的差值
                 */
                $maxDiff = max($maxDiff, $diff-abs($num3[$s]-$target), $diff-abs($num3[$e]-$target));
            } else {}
        }

        /**
         * @return
         */
        return ($diffSum - $maxDiff) % (pow(10, 9)+7);
    }
}

/**
 * @url http://127.0.0.1/leetcode/202107/2021.07.14.php
 */
$datas = [
    [[1,7,5], [2,3,5]], // 3
    [[2,4,6,8,10], [2,4,6,8,10]], // 0
    [[1,10,4,4,2,7], [9,3,5,1,7,4]], // 20
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->minAbsoluteSumDiff(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
